// pages/recruit/postJob/postJob.js
let City = require('../../../utils/allcity.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 基本参数
    companyName_text: '',
    type_id: '', //职位id
    type_name: '', //职位类型
    Title_text: '',
    platform_id: "0",
    platform_text: '',
    platfrom_all: '', //所有平台
    area_text: '',
    salaryRange_text: '', //薪资范围
    BaseSalary_text: '',
    Name_text: '',
    lightspot: '',
    detailed_text: '', //职位描述
    welfare_text: '',

    job_method: 3,
    salary_type: 1,

    platform_text_arr: [],
    is_company: false,


    changed: false,
    recruit_id: '',



    //数据组
    platfrom: [],
    platfrom_nochange: [],
    recuitType: [],
    welfare: [],

    resume_id: '',

    // 选择地区 Start
    city: City,
    // 选择地区 End


    // 弹框开关 Start
    type_flag: true,
    platform_flag: true,
    welfare_flag: true,
    area_flag: true,
    base_flag: false,
    other_flag: true,
    discover: false,
    home: false,
    click_time: 0,
    lightspot_flag: true,
    // 弹框开关 End

    //滚轮
    multiArray: [],
    multiIndex: [0, 0],
    high: '2000',
    low: '1000',

  },
  click_lightspot(){
    this.setData({
      lightspot_flag:false,
    })
  },
  //剩余发布次数
  residue_time() {
    wx.request({
      url: app.globalData.url + 'api/recruit/limit/number',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let last_time = e.data.data.is_send;
        this.setData({
          last_time: last_time,
        })
      },
    })
  },
  //职位名称
  inp_Title(e) {
    let Title_text = e.detail.value;
    this.setData({
      Title_text: Title_text,
    })
    // console.log(e.detail.value)
  },
  //去协议页
  go_protocol() {
    wx.navigateTo({
      url: '../../../../../../protocol/protocol?url=https://www.zhibohome.net/fawu/postJob.html&title=直播之家职位信息发布规则',
    })
  },
  //提示填写
  click_attention() {
    wx.showToast({
      title: '请先填写公司信息',
      icon: 'none',
    })
  },
  //点击
  click_submit() {
    let click_time = this.data.click_time;
    click_time = click_time + 1
    if (click_time == 1) {
      this.submit();
    } else {
      console.log("你好，打桩机")
    }



  },
  //发布
  submit() {
    let that = this;
    this.setData({
      MASK: true,
    })
    wx.showLoading({
      title: '提交中！',
    })
    if (this.data.type_id != "" && this.data.Title_text != '' && this.data.salaryRange_text != "" && this.data.BaseSalary_text != "" && this.data.lightspot != "" && this.data.job_method != "" && this.data.detailed_text!='') {
      if (this.data.changed) {
        //更新
        var platform_id = this.data.platform_id;
        if (typeof(platform_id) == "string") {
          var job_plat = platform_id;
        } else {
          var job_plat = platform_id.join(",");
        }
        wx.request({
          url: app.globalData.url + 'api/guild/update/position/info',
          data: {
            token: app.globalData.token,
            recruit_id: this.data.recruit_id, //招聘ID
            type: 2,
            anchor_type: this.data.type_id,
            title: this.data.Title_text,
            job_plat: job_plat,
            work_position: this.data.area_text,
            job_method: this.data.job_method,
            pay: this.data.salaryRange_text,
            keep_pay: this.data.BaseSalary_text,
            salary_type: this.data.salary_type,
            lightspot: this.data.lightspot,
            job_description: this.data.detailed_text,
            welfare: this.data.welfare_text,
          },
          method: 'POST',
          success: function(e) {
            console.log(e.data.message)
            console.log(-1)
            if (e.data.message == "发布成功") {
              console.log(0)
              if (that.data.discover) {
                console.log(1)
                // wx.redirectTo({
                //   url: '../discoverAnchor/discoverAnchor',
                // })
                wx.hideLoading();
                wx.navigateBack({
                  delta: 1,
                })
              } else if (that.data.home) {
                console.log(2)
                wx.hideLoading();
                wx.navigateBack({
                  delta: 1,
                })
              } else {
                console.log(3)
                wx.hideLoading();
                wx.redirectTo({
                  url: '../jobList/jobList',
                })
              }
            } else if (e.data.message == "修改成功") {
              wx.hideLoading();
              wx.redirectTo({
                url: '../jobList/jobList',
              })
            } else {
              wx.showToast({
                title: '发布失败！',
                icon: 'none',
              })
            }

          },
        })
      } else {
        //首次
        var platform_id = this.data.platform_id;
        if (typeof(platform_id) == "string") {
          var job_plat = platform_id;
        } else {
          var job_plat = platform_id.join(",");
        }
        wx.request({
          url: app.globalData.url + 'api/guild/job/add',
          data: {
            token: app.globalData.token,
            anchor_type: this.data.type_id,
            title: this.data.Title_text,
            job_plat: job_plat,
            work_position: this.data.area_text,
            job_method: this.data.job_method,
            pay: this.data.salaryRange_text,
            keep_pay: this.data.BaseSalary_text,
            salary_type: this.data.salary_type,
            lightspot: this.data.lightspot,
            job_description: this.data.detailed_text,
            welfare: this.data.welfare_text,
          },
          method: 'POST',
          success: function(e) {
            console.log(e.data.message)
            console.log(-1)
            if (e.data.message == "发布成功") {
              console.log(0)
              if (that.data.discover) {
                console.log(1)
                wx.hideLoading();
                wx.redirectTo({
                  url: '../discoverAnchor/discoverAnchor?resume_id=' + that.data.resume_id,
                })
              } else if (that.data.home) {
                console.log(2)
                // wx.redirectTo({
                //   url: '../index/index',
                // })
                wx.hideLoading();
                wx.navigateBack({
                  delta: 1,
                })
              } else if (that.data.welfare) {
                wx.hideLoading();
                wx.navigateBack({
                  delta: 1,
                })
              } else {
                console.log(3)
                wx.hideLoading();
                wx.redirectTo({
                  url: '../jobList/jobList',
                })
              }
            } else {
              wx.showToast({
                title: '发布失败！',
                icon: 'none',
              })
            }
          },
        })
      }

    } else {
      wx.showToast({
        title: '必填项不能为空',
        icon: 'none',
      })
    }

  },
  //是否存在公司
  get_company() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/guild/company/add',
      data: {
        token: app.globalData.token,
        type: 2,
      },
      method: 'POST',
      success: function(e) {
        console.log(e.data.data.company);
        let company = e.data.data.company;
        if (company == "" || company == "未设置") {
          that.setData({
            is_company: false,
          })
        } else {
          that.setData({
            is_company: true,
            companyName_text: e.data.data.company.company_name
          })
        }
      }
    })
  },
  //薪资范围 滚轮动作
  bindMultiPickerColumnChange: function(e) {
    let that = this;
    // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var multiArray = this.data.multiArray;
    var multiIndex = this.data.multiIndex;

    multiIndex[e.detail.column] = e.detail.value;

    var multiArray = this.data.multiArray;
    var multiIndex = this.data.multiIndex;
    var column = e.detail.column;
    var value = e.detail.value;
    console.log(value)
    switch (column) {
      case 0:
        var _low = multiArray[0][value];
        // console.log(_low)
        this.setData({
          low: _low
        })
        break;
      case 1:
        var _high = multiArray[1][value];
        this.setData({
          high: _high
        })
        break;
      default:
        console.log("null")
    }
    this.setData({
      multiArray: multiArray,
      multiIndex: multiIndex,
    });
  },
  //薪资 确定
  bindMultiPickerChange() {
    console.log("点击确认")
    let high_old = this.data.high.toString();
    let high = high_old.replace("k", "000");
    let low_old = this.data.low.toString();
    let low = low_old.replace("k", "000");

    // console.log("low+" + low)
    // console.log("high+"+high)

    if (parseInt(low) > parseInt(high)) {
      // console.log("请重新输入");
      console.log("low+" + low)
      console.log("high+" + high)

      wx.showToast({
        title: '薪资范围错误',
        icon: "none"
      })
      this.setData({
        salaryRange_text: '',
      })
    } else {
      console.log("正常")
      this.setData({
        salaryRange_text: low + "-" + high,
      })
    }
    console.log(this.data.salaryRange_text)
  },

  //设置 K数
  get_salary() {
    let arr1 = [];
    let arr2 = [];

    for (var i = 1; i <= 49; i++) {
      arr1.push(i + "k")
    }
    for (var i = 2; i <= 50; i++) {
      arr2.push(i + "k")
    }
    let arr = [];
    arr.push(arr1)
    arr.push(arr2)
    this.setData({
      multiArray: arr
    })
  },
  //底薪
  inp_BaseSalary(e) {
    // console.log(e.detail.value)
    this.setData({
      BaseSalary_text: e.detail.value,
    })
  },
  //点击福利待遇
  only_welfare(e) {
    console.log(e);
    var index = e.currentTarget.dataset.welfareindex; //获取当前点击的下标    
    var name = e.currentTarget.dataset.name;
    var welfare = this.data.welfare; //选项集合    

    var welfare_text = this.data.welfare_text;

    if (welfare_text == "") {
      welfare_text = this.data.welfare_text + name;
      console.log(1)
    } else {
      if (welfare_text.indexOf(name) == "-1") {
        welfare_text = welfare_text + ',' + name;
        console.log(2)
      } else {
        if (welfare_text.split(",").length > 1) {
          console.log(welfare_text.length);
          welfare_text = welfare_text.replace(name + ',', "");
          welfare_text = welfare_text.replace(',' + name, "");
        } else {
          welfare_text = welfare_text.replace(name, "");
        }
      }
    }

    this.setData({
      welfare_text: welfare_text,
    })

    //选中状态
    if (welfare[index].checked) {
      welfare[index].checked = false; //改变当前选中的checked值
    } else {
      welfare[index].checked = true; //改变当前选中的checked值
    }

    this.setData({
      welfare: welfare,
    })
    console.log(welfare_text)
  },
  //点击亮点 Start
  only_lightspot(e) {
    console.log(e)
    let that = this;
    var lightspot = this.data.lightspot, lightspot_list = this.data.lightspot_list, _lightspot = e.currentTarget.dataset.name, index = e.currentTarget.dataset.index;
    // console.log(typeId[0])
    if (that.data.lightspot.split(",").length >= 3 && that.data.lightspot.split(",")[0] != _lightspot && that.data.lightspot.split(",")[1] != _lightspot && that.data.lightspot.split(",")[2] != _lightspot ) {
      wx.showToast({
        title: '最多选三个',
        icon: 'none',
      })
    } else {
      //平台文字
      if (lightspot == "") {
        lightspot = this.data.lightspot + _lightspot;
        // console.log(1)
      } else {
        if (lightspot.indexOf(_lightspot) == "-1") {
          lightspot = lightspot + ',' + _lightspot;
          // console.log(2)
        } else {
          if (lightspot.split(",").length > 1) {
            lightspot = lightspot.replace(_lightspot + ',', "");
            lightspot = lightspot.replace(',' + _lightspot, "");
          } else {
            lightspot = lightspot.replace(_lightspot, "");
          }
        }
      }
      this.setData({
        lightspot: '',
      })
      this.setData({
        lightspot: lightspot,
      })
      //选中状态
      // console.log(platfrom)
      if (lightspot_list[index].checked) {
        lightspot_list[index].checked = false; //改变当前选中的checked值 取消选择
      } else {
        lightspot_list[index].checked = true; //改变当前选中的checked值 确认选择
      }

      this.setData({
        lightspot_list: lightspot_list,
      })
    }
    console.log("id+" + this.data.type_id)
  },
  //点击亮点 End

  //点击选择职位类型
  only_recuitType(e) {
    console.log(e);
    var index = e.currentTarget.dataset.recuittypeindex; //获取当前点击的下标    
    var type_name = e.currentTarget.dataset.type_name;
    var type_id = e.currentTarget.dataset.id;
    var recuitType = this.data.recuitType; //选项集合    
    if (recuitType[index].checked) return; //如果点击的当前已选中则返回    
    recuitType.forEach(item => {
      item.checked = false
    })
    recuitType[index].checked = true; //改变当前选中的checked值    
    this.setData({
      recuitType: recuitType,
      type_name: type_name,
      type_id: type_id,
    })
    this.modal_hide();
  },
  // //点击全部平台
  // other_platfrom() {
  //   let that = this;
  //   this.platfrom_info();

  //   that.setData({
  //     other_flag: true,
  //     platform_text: "",
  //     platform_id: that.data.platfrom_all,
  //   })
  // },
  //点击选择平台
  only_platfrom(e) {
    let that = this;
    // console.log(this.data.platform_text);
    // this.setData({
    //   platform_id: "",
    // })

    // console.log(e);
    var index = e.currentTarget.dataset.platfromindex; //获取当前点击的下标
    var _plat_name = e.currentTarget.dataset.plat_name;
    var _plat_id = e.currentTarget.dataset.plat_id;
    var platfrom = this.data.platfrom; //选项集合
    var platfrom_nochange = this.data.platfrom_nochange

    var platform_text = this.data.platform_text;
    var platformId = this.data.platform_id;


    if (typeof(platformId) == "string") {
      var platform_id = platformId.split(",");
      console.log("11111111122222+" + typeof(platform_id))
    } else {
      var platform_id = platformId;
      console.log(22222222)
    }

    if (index == 0) {
      //点击 全部平台
      console.log("index=0");
      this.platfrom_info();
      that.setData({
        platform_text: '全平台',
        platform_id: '0',
        platfrom: platfrom,
      })
      console.log(that.data.platfrom_nochange)
    } else {
      //点击 除了全部平台
      platfrom[0].checked = false;
      that.setData({
        platform_text: '',
        platform_id: '',
      })
      //平台文字
      if (platform_text == "") {
        platform_text = this.data.platform_text + _plat_name;
        // console.log(1)
      } else {
        if (platform_text.indexOf(_plat_name) == "-1") {
          platform_text = platform_text + ',' + _plat_name;
          // console.log(2)
        } else {
          if (platform_text.split(",").length > 1) {
            // console.log(platform_text.length);
            platform_text = platform_text.replace(_plat_name + ',', "");
            platform_text = platform_text.replace(',' + _plat_name, "");
          } else {
            platform_text = platform_text.replace(_plat_name, "");
          }
        }
      }
      //平台数字
      if (platform_id == "") {
        platform_id.push(_plat_id);
        // console.log(1)
      } else {
        if (platform_id.indexOf(_plat_id) == "-1") {
          platform_id.push(_plat_id);
          // console.log(2)
        } else {
          that.remove(platform_id, _plat_id);
        }
      }
      if (platform_id.indexOf("0") != "-1") {
        console.log("有0")
        that.remove(platform_id, "0");
      }
      this.setData({
        platform_text: '',
        platform_id: '',

      })
      this.setData({
        platform_text: platform_text,
        platform_id: platform_id,
      })
      //选中状态
      if (platfrom[index].checked) {
        platfrom[index].checked = false; //改变当前选中的checked值 取消选择
      } else {
        platfrom[index].checked = true; //改变当前选中的checked值 确认选择
      }
      this.setData({
        platfrom: platfrom,
        other_flag: false,
      })
    }



    console.log("id+" + this.data.platform_id)
    console.log("id+" + this.data.platform_text)

  },
  //设置 原型
  indexOf(arr, val) {
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] == val) return i;
    }
    return -1;
  },
  //设置 原型
  remove(arr, val) {
    var index = arr.indexOf(val);
    if (index > -1) {
      arr.splice(index, 1);
    }
  },

  //获取职位类型
  recuitType_info() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/recruit/platfrom/type',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e.data.data.recuitType);

        //直播类型
        let recuitType_new = e.data.data.recuitType;
        let recuitType = [];
        recuitType_new.map(((item, index) => {
          recuitType.push(Object.assign(item, {
            checked: false
          }))
        }))
        that.setData({
          recuitType: recuitType,
        })
      },
    })
  },
  //获取直播平台
  platfrom_info() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/recruit/platfrom/type',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e.data.data.platfrom["0"].id);

        //平台数组
        // var platfrom_new = e.data.data.platfrom;
        var platfrom = e.data.data.platfrom;
        platfrom[0].checked = true;
        // var platfrom_all = "";
        // platfrom_new.map(((item, index) => {
        //   platfrom.push(Object.assign(item, {
        //     checked: false
        //   }))
        // }))
        // for (var i = 0; i < platfrom_new.length; i++) {
        //   platfrom_all = platfrom_all + platfrom_new[i].id + ","
        //   // console.log(platfrom_new)
        // }
        // platfrom_all = platfrom_all.substr(0, platfrom_all.length - 1)
        // // console.log(platfrom_all)
        that.setData({
          platfrom: platfrom,
          platfrom_nochange: e.data.data.platfrom
          // platform_id: platfrom_all,
          // platfrom_all: platfrom_all,
        })
        console.log(that.data.platfrom_nochange)
      },
    })
  },
  //获取职位详细信息
  job_info() {
    wx.request({
      url: app.globalData.url + 'api/guild/position/list',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
      },
    })
  },
  //福利信息
  welfare_info() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/welfare/list',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e.data.data.list)
        that.setData({
          welfare: e.data.data.list
        })
      },
    })
  },

  //去功能整合页
  go_function1() {
    wx.navigateTo({
      url: '../../mine/CompanyBasis/CompanyBasis?flag=company_flag',
    })
  },
  go_function2() {
    wx.navigateTo({
      url: '../function/function?flag=job_flag',
    })
  },

  //工作地区
  click_area() {
    this.setData({
      area_flag: false,
      base_flag: true,
    })
  },
  //福利待遇
  click_welfare() {
    let that = this;
    that.setData({
      welfare_flag: false,
    })
  },
  //结算周期
  click_salary(e) {
    console.log(e.currentTarget.dataset.salary_type)
    let that = this;
    let salary_type = e.currentTarget.dataset.salary_type;

    that.setData({
      salary_type: salary_type,
    })
  },

  // 工作范围
  click_method(e) {
    console.log(e.currentTarget.dataset.job_method)
    let that = this;
    let job_method = e.currentTarget.dataset.job_method;

    that.setData({
      job_method: job_method,
    })


  },

  //选择平台
  click_platform() {
    let that = this;
    that.setData({
      platform_flag: false,
    })
  },

  //选择职位类型
  click_type() {
    let that = this;
    if (this.data.changed) {
      console.log("不能动")
    } else {
      that.setData({
        type_flag: false,
      })
    }

  },

  // 关闭弹框 Start
  modal_hide() {
    this.setData({
      type_flag: true,
      platform_flag: true,
      welfare_flag: true,
      lightspot_flag: true,
    })
  },
  // 关闭弹框 End

  //获得亮点
  get_lightspot(){
    wx.request({
      url: app.globalData.url + 'api/recruit/change/lightspot',
      data: {
        token: app.globalData.token
      },
      method: 'POST',
      success: (e)=> {
        console.log(e)
        let lightspot_list = e.data.data.list;
        lightspot_list.forEach(item => {
          item.checked = false
        })
        this.setData({
          lightspot_list: lightspot_list,
        })
      }
    })
  },
  // 获取原有的职位信息
  get_jobInfo(_recruit_id) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/guild/update/position/info',
      data: {
        token: app.globalData.token,
        recruit_id: _recruit_id,
        type: 1,
      },
      method: 'POST',
      success: function(e) {
        console.log(e.data.data.list)
        var jobInfo = e.data.data.list,
          Title_text = e.data.data.list.title ? e.data.data.list.title : "";
        that.setData({
          type_id: e.data.data.list.anchor_type, //职位id
          // type_name: '', //职位类型
          platform_id: e.data.data.list.job_plat,
          Title_text: Title_text,
          // platform_text: '',
          area_text: e.data.data.list.work_position,
          salaryRange_text: e.data.data.list.pay_low + "-" + e.data.data.list.pay_high, //薪资范围
          BaseSalary_text: e.data.data.list.keep_pay,
          lightspot: e.data.data.list.lightspot,
          detailed_text: e.data.data.list.job_description, //职位描述
          welfare_text: e.data.data.list.welfare,

          job_method: e.data.data.list.job_method,
          salary_type: e.data.data.list.salary_type,
        })
      },
    })
  },









  // 选择地区 Start
  bindtap(e) {
    console.log(e.detail)
    this.setData({
      area_text: e.detail.name,
      area_flag: true,
      base_flag: false,
    })
  },
  input(e) {
    this.value = e.detail.value
  },
  searchMt() {
    // 当没有输入的时候，默认inputvalue 为 空字符串，因为组件 只能接受 string类型的 数据 
    if (!this.value) {
      this.value = '';
    }
    this.setData({
      value: this.value
    })
  },
  // 选择地区 End




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.discover) {
      this.setData({
        discover: true,
        resume_id: options.resume_id,
      })
    }
    if (options.home) {
      this.setData({
        home: true,
        resume_id: options.resume_id,
      })
      console.log("home")
    }
    if (options.recruit_id) {
      this.get_jobInfo(options.recruit_id);
      this.setData({
        recruit_id: options.recruit_id,
        type_name: options.type_name,
        platform_text: options.plat_name,
        changed: options.changed,
      })
    }
    if (options.welfare) {
      this.setData({
        welfare: true
      })
    }
    this.get_lightspot();
    this.job_info();
    this.platfrom_info();
    this.recuitType_info();
    this.welfare_info();
    this.get_salary();

    this.get_company();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})